const express = require('express');
const app = express();
var history = require('connect-history-api-fallback');

const port = process.env.PORT || 8080;

app.listen(port, function () {
    console.log('服务区启动成功，端口' + port);
});

app.use(history({
    rewrites: [
        {
            from: /^\/api\/.*$/,
            to: function (context) {
                return context.parsedUrl.path
            }
        }
    ]
}));

// 设置静态文件夹
app.use(express.static('public'));

// 解析请求体
app.use(express.json());
app.use(express.urlencoded({ extended: false }));

// 解析cookie
const cookieParser = require('cookie-parser');
app.use(cookieParser());

const userRouter = require('./router/user');
app.use('/api/user', userRouter);

// 除去用户登录和注册操作之外的所有接口，都必须携带token
// 使用中间件来解析请求中的token信息
const { verifyToken } = require("./utils/valitate");
app.use(verifyToken);

// 课程相关接口
const courseRouter = require('./router/course');
app.use('/api/course', courseRouter);

// 视频相关接口
const videoRouter = require('./router/video');
app.use('/api/video', videoRouter);

// 弹幕相关接口
const barrageRouter = require('./router/barrage');
app.use('/api/barrage', barrageRouter);

// 弹幕相关接口
const commentRouter = require('./router/comment');
app.use('/api/comment', commentRouter);

const menuRouter = require('./router/menu');
app.use('/api/menu', menuRouter);

const logRouter = require('./router/log');
app.use('/api/log', logRouter);